//
//  AddLeaveViewController.m
//  fitnessmanage
//
//  Created by 众拳科技 on 2017/11/2.
//  Copyright © 2017年 zongquankeji. All rights reserved.
//  添加请假

#import "AddLeaveViewController.h"
#import "AddLeaveView.h"
#import "MOFSPickerManager.h"
#import "ChosePayView.h"

@interface AddLeaveViewController ()<ChoseBtnDelegate>

@property(nonatomic,weak)AddLeaveView *addLeaveView;

@property(nonatomic,strong)NSMutableDictionary *leaveDict;

@end

@implementation AddLeaveViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.title = @"添加请假";
    
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(rightBtnClick)];
    [rightButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName:CSBlackColor,NSFontAttributeName:MidFont} forState:UIControlStateNormal];
    [rightButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName:CSBlackColor,NSFontAttributeName:MidFont} forState:UIControlStateHighlighted];
    
    self.navigationItem.rightBarButtonItem = rightButtonItem;
    
    [self createSubView];
}

-(void)createSubView{
    
    AddLeaveView *addLeaveView = [[AddLeaveView alloc] initWithFrame:self.view.frame];
    
    addLeaveView.startTimeView.choseBtn.tag = 1;
    addLeaveView.endTimeView.choseBtn.tag = 2;
    [addLeaveView.startTimeView.choseBtn addTarget:self action:@selector(showTimeView:)];
    [addLeaveView.endTimeView.choseBtn addTarget:self action:@selector(showTimeView:)];
    
    addLeaveView.choseBtnDelegate = self;
    
    [self.view addSubview:addLeaveView];
    
    self.addLeaveView = addLeaveView;
    
    [addLeaveView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.edges.mas_equalTo(self.view);
    }];
}

-(void)showTimeView:(UIButton *)btn{
    
    [self.view endEditing:YES];
    
    __weak typeof(self)weakSelf = self;
    
    NSInteger tag = btn.tag;
    
    [[MOFSPickerManager shareManger] showDatePickerWithTag:1020 title:@"选择日期" cancelTitle:@"取消" commitTitle:@"确定" datePickerMode:UIDatePickerModeDate commitBlock:^(NSDate *date) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];//创建一个日期格式化器
            
            dateFormatter.dateFormat=@"yyyy-MM-dd";//指定转date得日期格式化形式
            
            if (tag == 1) {

                weakSelf.addLeaveView.startTimeView.choseLabel.text = [dateFormatter stringFromDate:date];
                weakSelf.addLeaveView.startTimeView.choseLabel.textColor = TFColor;
                
                [weakSelf.leaveDict setObject:[dateFormatter stringFromDate:date] forKey:@"BeginDate"];
            }
            else{

                weakSelf.addLeaveView.endTimeView.choseLabel.text = [dateFormatter stringFromDate:date];
                weakSelf.addLeaveView.endTimeView.choseLabel.textColor = TFColor;
                
                [weakSelf.leaveDict setObject:[dateFormatter stringFromDate:date] forKey:@"EndDate"];
            }
            
        });
        
    } cancelBlock:^{
        
        
    }];
}

#pragma mark 支付方式选择
-(void)chosePayBtnClick{
    
    [self.view endEditing:YES];
    
    __weak typeof(self)weakSelf = self;
    
    ChosePayView *chosePayView = [[ChosePayView alloc] init];
    
    [chosePayView showWithAnimation];
    
    chosePayView.commitBlock = ^(NSString *str,NSString *strId) {
      
        weakSelf.addLeaveView.payStyleBtnView.choseLabel.text = str;
        
        weakSelf.addLeaveView.payStyleBtnView.choseLabel.textColor = TFColor;
        
        [weakSelf.leaveDict setObject:strId forKey:@"PayType"];
    };
}

#pragma mark 右侧按钮点击
-(void)rightBtnClick{
    
    //原因
    NSString *reason = self.addLeaveView.reasonInputView.titleTF.text;
    
    //开始日期
    NSString *beginDate = [self.leaveDict objectForKey:@"BeginDate"];
    
    //结束日期
    NSString *endDate = [self.leaveDict objectForKey:@"EndDate"];
    
    if (!reason || !beginDate || !endDate) {
        
        [MBProgressHUD showText:@"请输入完整信息"];
        
        return;
    }
    
    VenueModel *venueModel = [VenueModel venue];
    
//    [self.leaveDict setObject:venueModel.venueId forKey:@"ShopID"];
//    
//    [self.leaveDict setObject:self.memberCardId forKey:@"MemberCardID"];
//    
//    [self.leaveDict setObject:reason forKey:@"Reason"];
    
    [self.leaveDict setObject:self.addLeaveView.chargeSwitchView.titleSwitch.isOn?@"1":@"0" forKey:@"IsCharge"];
    
    if (self.addLeaveView.commonInputView) {
        
        [self.leaveDict setObject:self.addLeaveView.commonInputView.titleTF.text forKey:@"Fee"];
    }
    
    [self.leaveDict setValue:self.addLeaveView.remarkBtnView.titleTF.text forKey:@"Memo"];
    
    [MBProgressHUD showMessage:@"请求中..." toView:self.view];
    
    [HTTPTool POSTRaw:Post_Leave_LeaveOperate parameters:self.leaveDict
     
              success:^(id responseObject){
                  
                  [MBProgressHUD hideHUDForView:self.view];
                  
                  if (responseObject && [[responseObject objectForKey:@"Code"] intValue] == 0) {
                      
                      [MBProgressHUD showText:@"添加成功"];
                      
                      [NotificationCenter postNotificationName:@"ReloadLeave" object:nil];
                      
                      [self.navigationController popViewControllerAnimated:YES];
                  }
                  
              }
     
              failure:^(NSError *error){
                  
                  [MBProgressHUD hideHUDForView:self.view];
              }];
}


#pragma mark 请假
-(NSMutableDictionary *)leaveDict{
    
    if (!_leaveDict) {
        
        _leaveDict = [[NSMutableDictionary alloc] init];
    }
    
    return _leaveDict;
}
@end
